<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class AccountController extends Zenddn_Controller_Action{
    
    
    public function init(){
        parent::init();
        parent::loadTemplate('magashop');
        $this->view->page = "account";
        $layout = $this->_helper->layout()->setLayout('product-detail');
    }
    
    public function indexAction(){
        $info = $this->checkLogin();
        
        $request = $this->_request;
        
        $id = $info->getId();
        
        $cache = new Zenddn_System_Cache();
        $cache = $cache->getCache();
        $cache_id = "user_getData_{$id}";
        
        if(($user = $cache->load($cache_id)) === false){
            $model = new Default_Model_Account();
            $user = $model->getData($id);
            
            $cache->save($user, $cache_id, array('user'));
        }
        
        $this->view->data = $user;
        
        if($request->isPost()){
            $validator = new Default_Form_Account($request->getParams(), $user);
            
            if($validator->isError()){
                $this->view->error = $validator->getMessageError();
                $this->view->data = $validator->getData();
            }else{
                $model->saveData($validator->getData());
                
                $this->_helper->_redirector->gotoRoute(array(), 'account', true);
            }
        }
        
        $this->view->title = 'account info';
        $this->view->edit = true;
    }
    
    public function orderAction(){
    	$info = $this->checkLogin();
        
        $model = new Default_Model_Order();
        $order_purchase = $model->getDataByEmail($info->getEmail(), true);
        $order_notpurchase = $model->getDataByEmail($info->getEmail());
        
        $this->view->order_purchase = $order_purchase;
        $this->view->order_notpurchase = $order_notpurchase;
    }
    
    private function checkLogin(){
        $layout = $this->_helper->layout()->setLayout('account');
        
        $info = new Zenddn_System_Info();
        
        if(!$info->checkLogin()){
            $this->_helper->_redirector->gotoRoute(array(), 'account_login', true);
        }
        
        return $info;
    }
    
    public function loginAction(){
        $request = $this->_request;
    	
        if($request->isPost()){
            $validator = new Default_Form_AccountLogin($request->getParams());
            
            $error = false;
            
            if($validator->isError()){
                $error = true;
            }else{
                $auth = new Zenddn_System_Auth();
                
                if($auth->login($validator->getData(), false)){
                    $this->_helper->_redirector->gotoRoute(array(), 'home', true);
                }else{
                    $error = true;
                }
            }
            
            $this->view->error = $error;
        }
    }
    
    public function logoutAction(){
    	$auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        
        $this->_helper->_redirector->gotoRoute(array(), 'home', true);
    	$this->_helper->viewRenderer->setNoRender();
    }
    
    public function registerAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $validator = new Default_Form_Account($request->getParams());
            
            if($validator->isError()){
                $this->view->error = $validator->getMessageError();
                $this->view->data = $validator->getData(); 
            }else{
                $this->_helper->viewRenderer->setNoRender();
                
                $data = $validator->getData();
                
                $model = new Default_Model_Account();
                $model->saveData($data);
                
                $this->view->name = $data['name'];
                $this->view->email = $data['email'];
                $this->view->code = sha1($data['email'] . "&" . $data['username']);
                
                $content = $this->view->render('email/register.phtml');
                
                $mail = new Zenddn_Mail_Mail();
                $mail->sendActivationCode($data['email'], $data['name'], $content);
                
                $url = $this->view->url(array(), 'account_success', true) . "?type=register";
                $this->_helper->_redirector->gotoUrl($url);
            }
        }
        
        $this->view->title = 'create account';
    }
    
    public function activeAction(){
    	$request = $this->_request;
        
        $email = $request->getParam('email', false);
        $activeCode = $request->getParam('activecode', false);
        
        if($email && $activeCode){
            $model = new Default_Model_Account();
            $result = $model->checkActiveCode($email, $activeCode);
            
            $type = "activeunsuccess";
            
            if($result){
                $type = "activesuccess";
            }
        }
        
        $url = $this->view->webinfo('url') . $this->view->url(array(), 'account_success', true) . "?type=$type";
        $this->_helper->_redirector->gotoUrl($url);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function successAction(){
    	$request = $this->_request;
        
        $type = $request->getParam('type', 'register');
        
        $title = "";
        $message = "";
        
        if($type == 'register'){
            $title = 'create account success';
            $message = 'create account success message';
        }else if($type == 'activesuccess'){
            $title = 'active account success';
            $message = 'active account success message';
        }else if($type == 'activeunsuccess'){
            $title = 'active account unsuccess';
            $message = 'active account unsuccess message';
        }else if($type == 'getpassword'){
            $title = 'reget password';
            $message = 'reget password message';
        }else if($type == 'resetpasswordunsuccess'){
            $title = 'reset password';
            $message = 'reset password unsuccess message';
        }else if($type == 'resetpasswordsuccess'){
            $title = 'reset password';
            $message = 'reset password success message';
        }
        
        $this->view->title = $title;
        $this->view->message = $message;
    }
    
    public function forgotPasswordAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $email = $request->getParam('email');
            
            $model = new Default_Model_Account();
            
            if($model->checkEmailExists($email)){
                $user = $model->getDataByEmail($email);
                
                $this->view->username = $user['username'];
                $this->view->name = $user['name'];
                $this->view->email = $user['email'];
                $this->view->code = sha1($user['email'] . "&" . $user['username']);
                
                $content = $this->view->render('email/forgotpassword.phtml');
                
                $mail = new Zenddn_Mail_Mail();
                $mail->sendResetPasswordCode($user['email'], $user['name'], $content);
                
                $url = $this->view->webinfo('url') . $this->view->url(array(), 'account_success', true) . "?type=getpassword";
                $this->_helper->_redirector->gotoUrl($url);
            }else{
                $error['email'] = "email error";
                
                $this->view->error = $error;
            }
        }
    }
    
    public function getPasswordAction(){
    	$request = $this->_request;
        
        $email = $request->getParam('email', false);
        $resetCode = $request->getParam('resetcode', false);
        
        $strUrl = $this->view->webinfo('url') . $this->view->url(array(), 'account_success', true) . "?type=";
        
        $type = "resetpasswordunsuccess";
        $url = $strUrl . $type;
        
        if($email && $resetCode){
            $model = new Default_Model_Account();
            $result = $model->checkResetCode($email, $resetCode);
            
            if($result){
                $this->view->title = "reset password";
                $this->view->message = "your password is";
                $this->view->password = $result;
            }else{
                $this->_helper->_redirector->gotoUrl($url);
            }
        }else{
            $this->_helper->_redirector->gotoUrl($url);
        }
    }
}